package managesystem;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import IO.Hash;
import controlsystem.Course;
import controlsystem.CourseDescription;
import controlsystem.CourseManager;

/**
 * Registreer een nieuwe student
 * @author Incalza Dario
 *
 */
public class RegisterStudentServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	/**
	 * Maak de nieuwe student aan.
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse resp){
			String un = req.getParameter("username");
			String pw = req.getParameter("password");
			String fname = req.getParameter("firstname");
			String lname = req.getParameter("lastname");
			String email = req.getParameter("email");
			
			Student s = new Student(fname,lname,email,un,Hash.getHash(pw));
			
			String[] coursenames = req.getParameterValues("courses");
		
			List<CourseDescription> availableCourses=CourseManager.findAll();
		
			for(int i = 0; i < coursenames.length;i++){
				for(CourseDescription c: availableCourses){
					if(c.getName().equals(coursenames[i])){
						Course cobj = CourseManager.createCourseObj(c.getId());
						s.addCourse(cobj);
					}
				}
			}
			
			StudentManager.add(s);
			HttpSession session = req.getSession(true);
			session.setAttribute("message","Registered succesfully. Please login to start using the application.");
			try {
				resp.sendRedirect("index.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	

}
